<template>
	<view class="orderModule" :style="`padding-top:calc(${biasTop}px + 88rpx);`">
		<view class="navTop center" :style="`padding-top:${biasTop}px;`">
			<view class="n-back" @click="backFn">
				<image :src="`${globalData.masterDomain}/static/images/header_back.png?v=${globalData.imgVersion}`"
					mode="aspectFit">
				</image>
			</view>
			<text>我的订单</text>
		</view>
		<view class="list">
			<view class="item" :data-url="moduleList.length-5<index?`${item.url}`:item.url" data-type="0" @tap="goLink" v-for="(item, index) in moduleList"
				:key="index" :style="moduleList.length<=9&&index<=moduleList.length-5?`width:100%`:''">
				<view class="title">
					<image :src="item.icon" mode="aspectFill" />
					<text>{{ item.title }}</text>
				</view>
				<view class="num" v-if="orderTodo[item.code] > 0">{{ orderTodo[item.code] }}</view>
				<image :src="`${globalData.masterDomain}/templates/member/touch/images/arrow_icon2.png?v=${globalData.imgVersion}`" mode="aspectFit" class="rightArr" 
				v-else></image>
			</view>
		</view>
		<view class="btn" :data-url="`${globalData.appConfig.cfg_user_index}/homemaking-courierorder.html`" data-type="0" @tap="goLink">
			<view class="b-serve">
				<image :src="`${globalData.masterDomain}/templates/member/touch/images/orderimag/waimai.png?v=${globalData.imgVersion}`"></image>
				<text>我的服务单</text>
			</view>
			<view class="b-right">
				<text v-if="allMsg>0">{{allMsg}}</text>
				<image :src="`${globalData.masterDomain}/templates/member/touch/images/arrow_icon2.png?v=${globalData.imgVersion}`" mode="aspectFit"></image>
			</view>
		</view>
		<private_tip :privateShow="privateb"></private_tip>
	</view>
</template>

<script>
	//获取应用实例
	var _this;
	const app = getApp();
	export default {
		data() {
			return {
				globalData: app.globalData,
				state: 0,
				biasTop: 0,
				privateb: false,
				capsuleLeft: 0,
				allMsg:0,
				// extraData:[{
				// 	title:'排队',
				// 	url:`/order-business.html?type=paidui`,
				// 	icon:`${app.globalData.masterDomain}/static/images/business/paidui.png`
				// },{
				// 	title:'买单记录',
				// 	url:`/order-business.html?type=maidan`,
				// 	icon:`${app.globalData.masterDomain}/static/images/business/maidan.png`
				// },{
				// 	title:'点餐记录',
				// 	url:`/order-business.html?type=diancan`,
				// 	icon:`${app.globalData.masterDomain}/static/images/business/diancan.png`
				// },{
				// 	title:'订座记录',
				// 	url:`/order-business.html?type=dingzuo`,
				// 	icon:`${app.globalData.masterDomain}/static/images/business/dingzuo.png`
				// }],
				//订单状态  1待付款 2待使用 3待发货 4待收货 5待评价
				moduleList: [],
				orderTodo: [],
				initial:true, //首次进入
			};
		},
		async onLoad(options) {
			await this.$onLaunched; //onLaunch执行完再执行onload
			_this = this;
			if (app.globalData.wxprivate == -1) {
				app.watchG('wxprivate', function(value) {
					_this.wxprivate = value;
					if (value) { //弹出弹窗
						_this.privateb = true;
					} else {
						_this.onload(options);
					}
				})
			} else {
				if (app.globalData.wxprivate) { //弹出弹窗
					_this.privateb = true;
				} else {
					_this.onload(options);
				}
			}
			if (app.globalData.platForm == 'mp-weixin') {
				_this.biasTop = app.globalData.navTop + app.globalData.biasTop_88 //顶部偏差
			}
		},
		// 后退回来后，重新加载数据
		onShow() {
			if(!this.initial){
				this.checkLogin();
			}
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			this.checkLogin();
			uni.stopPullDownRefresh();
		},
		onShareAppMessage(res) {
			let config = app.globalData.appConfig;
			return {
				title: '我的订单',
				desc: config.share.description,
			};
		},
		onShareTimeline() {
			return {
				title: '我的订单',
			};
		},
		methods: {
			onload(options) {
				if (options && options.state) {
					const _state = options.state;
				} else {}
				_this.state = options.state;
				if (_this.initial) {
					this.checkLogin();
				}
				_this.pageData();
			},
			async pageData(){
				let data = {
				  service: 'homemaking',
				  action: 'orderList',
				  state: 10,
				  page: 1,
				  pageSize: 1,
				  dispatchid: app.globalData.userid,
				}
				let result = await _this.$ajax.post({
					url:app.encodeUrl(`include/ajax.php?`),
					data:data
				});
				if (result.state == 100) {
				  let pageInfo = result.info.pageInfo;
				  if (pageInfo.state10 > 0) {
				    _this.allMsg=pageInfo.state10;
				  }
				}
			},
			//验证是否登录
			checkLogin() {
				_this.initial=false;
				if (app.globalData.userid) {
					var userid = app.globalData.userid;
				} else {
					var userid = uni.getStorageSync('userid');
				}
				if (userid == null || userid == '') {
					uni.showModal({
						title: '提示',
						content: '您还未登录，请登录后查看订单！',
						confirmText: '去登录',
						success(res) {
							if (res.confirm) {
								app.tologin('/pages/member/order/index');
							} else {
								var referpage = getCurrentPages()[0].__displayReporter.showReferpagepath; //上个页面
								//前面有页面时
								if (referpage) {
									uni.navigateBack({
										delta: 1
									});
								} else {
									uni.redirectTo({
										url: '/pages/index/index'
									});
								}
							}
						}
					});
					return;
				} else {
					this.loadData();
				}
			},
			backFn() { //后退函数
				app.backFn();
			},
			//加载数据
			loadData() {
				uni.showLoading({
					title: '加载中...'
				});
				var dataPrarm = 'service=siteConfig&action=siteModule&type=0';
				var urlData = app.encodeUrl(dataPrarm);
				_this.$ajax.post({
					url: 'include/ajax.php?' + urlData,
				}).then(res => {
					uni.hideLoading();
					var data = res.data;
					if (data.state == 100) {
						var list = data.info;
						var moduleList = [];
						var iconUrl = app.globalData.masterDomain +
							'/templates/member/touch/images/orderimag/';
						var userDomain = app.globalData.appConfig.cfg_user_index + '/';
						for (var i = 0; i < list.length; i++) {
							var stateParam = '';
							var paotuiStateParam = '';
							var code = list[i].code;
							var state = _this.state;
							if (state == 1) {
								if (
									code == 'tuan' ||
									code == 'shop' ||
									code == 'waimai' ||
									code == 'paotui' ||
									code == 'integral' ||
									code == 'homemaking' ||
									code == 'travel' ||
									code == 'education'
								) {
									stateParam = '?state=0';
									paotuiStateParam = '?state=0';
								} else if (code == 'paimai') {
									stateParam = '?state=7';
								}
							}
							if (state == 2) {
								if (code == 'tuan' || code == 'travel' || code == 'education') {
									stateParam = '?state=1';
								} else if (code == 'shop') {
									stateParam = '?state=6,3';
								}
							}
							if (state == 3) {
								if (code == 'shop' || code == 'integral') {
									stateParam = '?state=1';
								} else if (code == 'awardlegou') {
									stateParam = '?state=5';
								} else if (code == 'paimai') {
									stateParam = '?state=6';
								}
							}
							if (state == 4) {
								if (code == 'tuan' || code == 'shop' || code == 'integral') {
									stateParam = '?state=6';
								} else if (code == 'homemaking') {
									stateParam = '?state=5';
								} else if (code == 'paimai') {
									stateParam = '?state=3';
								}
							}
							if (state == 5) {
								if (code == 'tuan' || code == 'shop') {
									stateParam = '?state=5';
								} else if (code == 'waimai') {
									stateParam = '?state=1';
									paotuiStateParam = '?state=8';
								}
							}
							let newParam = stateParam.includes('?') ? stateParam + '&mod=' + code : '?mod=' + code
							if (code == 'tuan') {
								moduleList.push({
									code: 'tuan',
									title: '团购订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'shop') {
								moduleList.push({
									code: 'shop',
									title: '商城订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'waimai') {
								moduleList.push({
									code: 'waimai',
									title: '外卖订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								}, {
									code: 'paotui',
									title: '跑腿订单',
									icon: iconUrl + 'paotui.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + paotuiStateParam + (paotuiStateParam.includes('?') ? '&mod=paotui' : '?mod=paotui')
								});
							} else if (code == 'integral') {
								moduleList.push({
									code: 'jfsc',
									title: '积分商城',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'homemaking') {
								moduleList.push({
									code: 'homemaking',
									title: '家政订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
								//家政服务人员
								// if (list[i].personal == 1) {
								// 	moduleList.push({
								// 		code: 'homemaking',
								// 		title: '我服务的订单',
								// 		icon: iconUrl + 'homemakingservice.png',
								// 		url: userDomain + 'homemaking-courierorder.html'
								// 	});
								// }
							} else if (code == 'travel') {
								moduleList.push({
									code: 'travel',
									title: '旅游订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'paimai') {
								moduleList.push({
									code: 'paimai',
									title: '拍卖订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'education') {
								moduleList.push({
									code: 'education',
									title: '教育订单',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							} else if (code == 'awardlegou') {
								moduleList.push({
									code: 'legou',
									title: '有奖乐购',
									icon: iconUrl + code + '.png' + '?v=' + _this.globalData.imgVersion,
									url: '/pages/packages/mcenter/order/order' + newParam
								});
							}
						}

						//商家服务
						var ucenterLinks = app.globalData.appConfig.ucenterLinks;
						if (ucenterLinks && ucenterLinks.includes('food')) {
							moduleList.push({
								code: 'diancan',
								title: '点餐记录',
								icon: app.globalData.masterDomain +
									'/static/images/business/diancan.png' + '?v=' + _this.globalData.imgVersion,
								url: userDomain + 'order-business.html?type=diancan'
							}, {
								code: 'dingzuo',
								title: '订座记录',
								icon: app.globalData.masterDomain +
									'/static/images/business/dingzuo.png' + '?v=' + _this.globalData.imgVersion,
								url: userDomain + 'order-business.html?type=dingzuo'
							}, {
								code: 'paidui',
								title: '排队记录',
								icon: app.globalData.masterDomain +
									'/static/images/business/paidui.png' + '?v=' + _this.globalData.imgVersion,
								url: userDomain + 'order-business.html?type=paidui'
							}, {
								code: 'maidan',
								title: '买单记录',
								icon: app.globalData.masterDomain +
									'/static/images/business/maidan.png' + '?v=' + _this.globalData.imgVersion,
								url: userDomain + 'order-business.html?type=maidan'
							});
						}
						_this.moduleList = moduleList;
						_this.getOrderTodo();
					}
				})
			},

			//获取待办数量
			getOrderTodo() {
				if (_this.state) {
					var dataPrarm = 'service=member&action=getModuleOrderList&state=' + _this.state;
					var urlData = app.encodeUrl(dataPrarm);
					_this.$ajax.post({
						url: 'include/ajax.php?' + urlData,
					}).then(res => {
						var data = res.data;
						if (data.state == 100) {
							var list = data.info;
							_this.orderTodo = list
						}
					})
				}
			},
			// 跳转链接
			goLink(e) {
				var currentTarget = e.currentTarget;
				var url = currentTarget.dataset.url;
				if (!url) {
					return false;
				}

				//原生页面
				if (currentTarget.dataset.type == 1 || url.indexOf('/pages/') == 0) {
					url = url.replace('miniprogram://', '/');
					uni.navigateTo({
						url: app.addUrlParam(url)
					});
					//小程序类型
				} else if (currentTarget.dataset.type == 2) {
					var ahref = url.replace('wxMiniprogram://', '');
					var miniId = ahref.split('?/')[0];
					var //小程序原始id
						path = ahref.split('?/')[1]; //跳转的路径
					var _ghId = miniId.split('/')[0];
					var _appid = miniId.split('/')[1];
					var _path = path ? '/' + path : '';
					uni.navigateToMiniProgram({
						appId: _appid,
						path: _path,
						success(res) {
							// wx.navigateBack();
						},
						fail(res) {
							uni.showToast({
								title: res,
								icon: 'success',
								duration: 2000
							});
						}
					});

					//H5链接
				} else {
					if (url.indexOf('work.weixin.qq.com/kfid') > -1) {
						//小程序跳转客服
						var link = url.split('?id=')[0];
						var //客服链接
							id = url.split('?id=')[1]; //企业ID
						var path = '/pages/openkf/openkf?url=' + encodeURIComponent(link) + '&id=' + id;
						if (link && id) {
							uni.openCustomerServiceChat({
								extInfo: {
									url: link
								},
								corpId: id,
								success(res) {
									console.log(res);
								}
							});
						} else {
							uni.navigateTo({
								url: '/pages/redirect/index?url=' + encodeURIComponent(app.addUrlParam(url))
							});
						}
					} else {
						uni.navigateTo({
							url: '/pages/redirect/index?url=' + encodeURIComponent(app.addUrlParam(url))
						});
					}
				}
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>